﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

public partial class admin_files : System.Web.UI.Page
{
    private CMyLink lnkDel;
    private CMyLink lnkNav;
    private CMyLink lnkSort;

    protected void Page_Load(object sender, EventArgs e)
    {
        lnkDel = new CMyLink();
        this.Controls.Add(lnkDel);
        lnkDel.Click += new EventHandler(lnkDel_Click);

        lnkNav = new CMyLink();
        this.Controls.Add(lnkNav);
        lnkNav.Click += new EventHandler(lnkNav_Click);

        lnkSort = new CMyLink();
        this.Controls.Add(lnkSort);
        lnkSort.Click += new EventHandler(lnkSort_Click);

        if (!IsPostBack)
        {
            //初始化类型列表
            CFile f = new CFile();
            DataTable dt = f.ReadFiles(0);
            if (null != dt)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    this.ddlType.Items.Add(new ListItem(dt.Rows[i]["title"].ToString(), dt.Rows[i]["id"].ToString()));
                }
            }
            InitTable(1, 20);
        }
    }

    void lnkSort_Click(object sender, EventArgs e)
    {
        string[] pars = (e as CMyLinkEventArg).Parameter.Split('/');
        if (pars[0] == "t")
        {
            //上升
            CFile f = new CFile();
            f.Sort(Convert.ToInt32(pars[1]), true);
        }
        else
        {
            //下降
            CFile f = new CFile();
            f.Sort(Convert.ToInt32(pars[1]), false);
        }
        InitTable(Convert.ToInt32(pars[2]), 20);
    }

    void lnkNav_Click(object sender, EventArgs e)
    {
        InitTable(Convert.ToInt32((e as CMyLinkEventArg).Parameter), 20);
    }

    void lnkDel_Click(object sender, EventArgs e)
    {
        string[] pars = (e as CMyLinkEventArg).Parameter.Split('/');
        CFile f = new CFile();
        f.Delete(Convert.ToInt32(pars[0]));
        InitTable(Convert.ToInt32(pars[1]), 20);
    }

    private void InitTable(int page, int count)
    {
        CFile f = new CFile();
        DataTable dt = f.ReadFiles(Convert.ToInt32(this.ddlType.SelectedValue));
        StringBuilder sb = new StringBuilder();
        if (null != dt)
        {
            int top = page * count;
            if (top > dt.Rows.Count)
            {
                top = dt.Rows.Count;
            }
            int c = 0;
            for (int i = (page - 1) * count; i < top; i++)
            {
                if (c % 2 == 0)
                {
                    sb.Append("<tr>");
                }
                else
                {
                    sb.Append("<tr bgcolor=\"#f5f5f5\">");
                }
                sb.Append("<td>" + i.ToString() + "</td>");
                sb.Append("<td>" + dt.Rows[i]["title"].ToString() + "</td>");
                sb.Append("<td>");
                if (dt.Rows[i]["path"].ToString() != "")
                {
                    sb.Append(dt.Rows[i]["path"].ToString());
                }
                else
                {
                    sb.Append("暂未上传");
                }
                sb.Append("</td>");
                sb.Append("<td>");
                sb.Append("<a href=\"javascript:uploadfile(" + dt.Rows[i]["id"].ToString() + ");\">更新文件</a>&nbsp;&nbsp;");
                sb.Append("<a href=\"" + Page.ClientScript.GetPostBackClientHyperlink(lnkDel, dt.Rows[i]["id"].ToString() + "/" + page.ToString()) + "\" onclick=\"return confirm('确认要删除吗?');\">删除</a>&nbsp;&nbsp;");
                sb.Append("<a target=\"_blank\" href=\"file_edit.aspx?s=" + this.ddlType.SelectedValue + "&id=" + dt.Rows[i]["id"].ToString() + "\">编辑</a>&nbsp;&nbsp;");
                if (i < top - 1)
                {
                    sb.Append("<a href=\"" + Page.ClientScript.GetPostBackClientHyperlink(lnkSort, "f/" + dt.Rows[i]["id"].ToString() + "/" + page.ToString()) + "\">下降</a>&nbsp;&nbsp;");
                }
                else
                {
                    sb.Append("下降&nbsp;&nbsp;");
                }
                if (i > 0)
                {
                    sb.Append("<a href=\"" + Page.ClientScript.GetPostBackClientHyperlink(lnkSort, "t/" + dt.Rows[i]["id"].ToString() + "/" + page.ToString()) + "\">上升</a>&nbsp;&nbsp;");
                }
                else
                {
                    sb.Append("上升&nbsp;&nbsp;");
                }
                sb.Append("</td>");
                sb.Append("</tr>");
                c++;
            }
            this.ltlTable.Text = sb.ToString();
            this.ltlNav.Text = CNavigator.CreateNavigator(page, count, dt.Rows.Count, Page, lnkNav);
            this.upTable.Update();
        }
        else
        {
            this.ltlTable.Text = "<tr><td colspan=\"4\" align=\"center\">暂无数据</td></tr>";
            this.ltlNav.Text = CNavigator.CreateNavigator(page, count, 0, Page, lnkNav);
            this.upTable.Update();
        }
    }

    protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
    {
        InitTable(1, 20);
    }
}